package service.impl;

import pojo.DoctorWork;
import dao.DoctorWorkDao;
import service.DoctorWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DoctorWorkServiceImpl implements DoctorWorkService {

    @Autowired
    private DoctorWorkDao doctorWorkDao;

    @Override
    public int addDoctorWork(DoctorWork doctorWork) {
        return doctorWorkDao.addDoctorWork(doctorWork);
    }

    @Override
    public int deleteDoctorWork(String day, String docId) {
        return doctorWorkDao.deleteDoctorWork(day, docId);
    }

    @Override
    public int updateDoctorWork(DoctorWork doctorWork) {
        return doctorWorkDao.updateDoctorWork(doctorWork);
    }

    @Override
    public DoctorWork getDoctorWorkByDayAndDocId(String day, String docId) {
        return doctorWorkDao.getDoctorWorkByDayAndDocId(day, docId);
    }

    @Override
    public List<DoctorWork> getDoctorWorksByDocId(String docId) {
        return doctorWorkDao.getDoctorWorksByDocId(docId);
    }

    @Override
    public List<DoctorWork> getDoctorWorksByDay(String day) {
        return doctorWorkDao.getDoctorWorksByDay(day);
    }

    @Override
    public List<DoctorWork> getAllDoctorWorks() {
        return doctorWorkDao.getAllDoctorWorks();
    }

    @Override
    public DoctorWork getDoctorWorkCount(String day, String docId) {
        return doctorWorkDao.getDoctorWorkCount(day, docId);
    }

    @Override
    public boolean updateCountLeft(String day, String docId, Integer countLeft) {
        return doctorWorkDao.updateCountLeft(day, docId, countLeft) > 0;
    }

    @Override
    public boolean updateOrderedCount(String day, String docId, Integer orderedCount) {
        return doctorWorkDao.updateOrderedCount(day, docId, orderedCount) > 0;
    }

    @Override
    @Transactional
    public boolean bookAppointment(String day, String docId) {
        DoctorWork work = doctorWorkDao.getDoctorWorkByDayAndDocId(day, docId);
        if (work == null || work.getCountLeft() <= 0) {
            return false;
        }

        int newCountLeft = work.getCountLeft() - 1;
        int result1 = doctorWorkDao.updateCountLeft(day, docId, newCountLeft);

        int newOrderedCount = (work.getOrderedCount() == null ? 0 : work.getOrderedCount()) + 1;
        int result2 = doctorWorkDao.updateOrderedCount(day, docId, newOrderedCount);

        return result1 > 0 && result2 > 0;
    }

    @Override
    @Transactional
    public boolean cancelAppointment(String day, String docId) {
        DoctorWork work = doctorWorkDao.getDoctorWorkByDayAndDocId(day, docId);
        if (work == null || (work.getOrderedCount() != null && work.getOrderedCount() <= 0)) {
            return false;
        }

        int newCountLeft = work.getCountLeft() + 1;
        int result1 = doctorWorkDao.updateCountLeft(day, docId, newCountLeft);

        int newOrderedCount = (work.getOrderedCount() == null ? 0 : work.getOrderedCount()) - 1;
        int result2 = doctorWorkDao.updateOrderedCount(day, docId, newOrderedCount);

        return result1 > 0 && result2 > 0;
    }

    @Override
    @Transactional
    public List<DoctorWork> getDoctorWorksByParentDeptId(Integer deptId) {
        System.out.println("Service层接收到的deptId: " + deptId);
        return doctorWorkDao.selectDoctorWorkByDeptId(deptId);
    }
}